<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/10/21 12:12
 */

namespace App\Service\Amount;


use App\Constants\User\UserAmountTypeConst;
use App\Dao\Users\UserCategoryDao;
use App\Dao\Users\UsersAccountDao;
use App\Dao\Users\UsersAmountLogsDao;
use App\Exception\ServiceException;
use App\Model\Users\UsersAmountLog;
use App\Utils\AmountUtils;
use Hyperf\Di\Annotation\Inject;

class GroupService
{

    #[Inject]
    protected UsersAccountDao $usersAccountDao;


    #[Inject]
    protected UsersAmountLogsDao $usersAmountLogsDao;


    #[Inject]
    protected UserCategoryDao $userCategoryDao;



    public function groupByType(array $params)
    {
        $userInfo = auth()->user();

        $accountId = 0;
        if(!empty($params['account_id']))
        {
            $accountInfo = $this->usersAccountDao->getInfoByPk($params['account_id']);

            if(!$accountInfo) throw new ServiceException('账册不存在');
            $accountId = $accountInfo->id;
        }
        $startTime = $params['start_date'];
        $endTime = date('Y-m-d H:i:s',strtotime($params['end_date']) + 86399);
        $groupType = $this->usersAmountLogsDao->groupByType($userInfo->id,$startTime,$endTime,$accountId)->toArray();
        $mapGroupAmount  = array_column($groupType,'total_amount','type');
        $inAmount = $mapGroupAmount[UserAmountTypeConst::INCOME->value] ?? 0;
        $outAmount = $mapGroupAmount[UserAmountTypeConst::EXPENSE->value] ?? 0;
        $nowAmount = $inAmount - $outAmount;
        return [
            AmountUtils::fenToyuan($inAmount),
            AmountUtils::fenToyuan($outAmount),
            AmountUtils::fenToyuan($nowAmount),
        ];
    }


    public function groupByAccount(array $params)
    {
        $userInfo = auth()->user();

        $startTime = $params['start_date'];
        $endTime = date('Y-m-d H:i:s',strtotime($params['end_date']) + 86399);
        $type = $params['type'] ?? 0;
        $groupAccount = $this->usersAmountLogsDao->groupByAccount($userInfo->id,$startTime,$endTime,$type);
        $responseAccount = $groupAccount->toArray();

        $accountIds = array_column($responseAccount,'account_id');

        $accountData = $this->usersAccountDao->getAccountByIds($userInfo->id,$accountIds)->toArray();

        $mapAccount = array_column($accountData,null,'id');

        $sourceData = [];
        foreach($responseAccount as &$item)
        {
            $totalAmount = AmountUtils::fenToYuan($item['total_amount']);
            $sourceData[] = [$mapAccount[$item['account_id']]['account_name'] ?? '',$totalAmount];
        }
        return $sourceData;
    }

    public function groupByCategory(array $params)
    {
        $userInfo = auth()->user();

        $accountId = 0;
        if(!empty($params['account_id']))
        {
            $accountInfo = $this->usersAccountDao->getInfoByPk($params['account_id']);

            if(!$accountInfo) throw new ServiceException('账册不存在');
            $accountId = $accountInfo->id;
        }
        $type = $params['type'] ?? 0;

        $startTime = $params['start_date'];
        $endTime = date('Y-m-d H:i:s',strtotime($params['end_date']) + 86399);

        $groupCategory = $this->usersAmountLogsDao->groupByCategory($userInfo->id,$startTime,$endTime,$type,$accountId);
        $responseCategory = $groupCategory->toArray();
        $categoryIds = array_column($responseCategory,'cate_id');
        $totalAmount = array_sum(array_column($responseCategory,'total_amount'));

        $categories = $this->userCategoryDao->getCateByIds($categoryIds);

        $mapCategory = array_column($categories,null,'id');

        foreach($responseCategory as &$item)
        {
            $item['per_cent'] = $totalAmount > 0 ?number_format(($item['total_amount']/$totalAmount)*100,2) : 0;
            $item['total_amount'] = AmountUtils::fenToYuan($item['total_amount']);
            $item['cate_name'] = $mapCategory[$item['cate_id']]['name'] ?? '';
            $item['cate_icon'] = $mapCategory[$item['cate_id']]['icon'] ?? '';
        }

        return $responseCategory;

    }
}